package com.hndzyu.yd.customtask;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.hndzyu.yd.R;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.widget.CountdownTextView;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by YJ on 2018/10/23.
 */
public class ScreenAdapter extends RecyclerView.Adapter<ScreenAdapter.ViewHolder> {
    private Context mContext;
    private List<ScreenBean.DataBean> mData = new ArrayList<>();

    public ScreenAdapter(Context context) {
        mContext = context;
    }
    public ScreenAdapter(Context context,List<ScreenBean.DataBean> mData) {
        this.mData=mData;
        mContext = context;
    }
    public void updateData(List<ScreenBean.DataBean> dataSet) {
        if (dataSet != null && !dataSet.isEmpty()) {
            mData=dataSet;
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_screen, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final ScreenBean.DataBean dataBean = mData.get(position);
        holder.tvText.setText(dataBean.getTname());
        //1下载  2注册  3其他
        switch (dataBean.getType()) {
            case 1:
                if (dataBean.getFromtype()==1){
                    holder.tvType.setText("下载-平台发布");
                }else {
                    holder.tvType.setText("下载-用户发布");
                }
                break;
            case 2:
                if (dataBean.getFromtype()==1){
                    holder.tvType.setText("注册-平台发布");
                }else {
                    holder.tvType.setText("注册-用户发布");
                }
                break;
            case 3:
                if (dataBean.getFromtype()==1){
                    holder.tvType.setText("其他-平台发布");
                }else {
                    holder.tvType.setText("其他-用户发布");
                }
                break;
            case 4:
                holder.tvType.setText("信用卡-平台发布");
                break;
        }

        if (dataBean.getIsvip()==1){
            holder.ivVip.setVisibility(View.VISIBLE);
        }else {
            holder.ivVip.setVisibility(View.GONE);
        }
        holder.tvMoney.setText(dataBean.getDan() + "元");
        Glide.with(mContext).load(Path.img(dataBean.getLogo())).apply(new RequestOptions().error(R.mipmap.logo)).into(holder.ivImg);
        if (dataBean.getNum()!=-1) {
            holder.tvNum.setText("剩余:" + dataBean.getNum() + "份");
        }else {
            holder.tvNum.setText("");
//            Glide.with(mContext).load(Path.img(dataBean.getLogo())).into(holder.ivImg);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mItemClickListener.onItemClick(dataBean);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_img)
        ImageView ivImg;
        @BindView(R.id.iv_isvip)
        ImageView ivVip;
        @BindView(R.id.tv_text)
        TextView tvText;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.timer)
        CountdownTextView timer;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    protected ScreenItemListener mItemClickListener;

    public interface ScreenItemListener {
        void onItemClick(ScreenBean.DataBean item);

    }

    public void setScreenItemListener(ScreenItemListener listener) {
        this.mItemClickListener = listener;
    }
}
